<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ] 
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/3/12-14:55
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\wechat\controller;


    use app\wechat\service\WechatService;
    use think\facade\Response;

    /**
     * Class Js
     * @package app\wechat\controller
     */
    class Js extends ChatBase
    {

        /**
         * @return \think\response
         * @throws \WeChat\Exceptions\InvalidResponseException
         * @throws \WeChat\Exceptions\LocalCacheException
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: index
         * @describe:
         */
        public function index()
        {
            $url          = $this->request->server('http_referer', $this->request->url(true), null);

            $wechat       = WechatService::getWebOauthInfo($url, $this->request->get('mode', 1), false);

            $openid       = isset($wechat['openid']) ? $wechat['openid'] : '';

            $unionid      = empty($wechat['fansinfo']['unionid']) ? '' : $wechat['fansinfo']['unionid'];

            $configJson   = json_encode(WechatService::getWebJssdkSign($url), JSON_UNESCAPED_UNICODE);

            $fansinfoJson = json_encode(isset($wechat['fansinfo']) ? $wechat['fansinfo'] : [], JSON_UNESCAPED_UNICODE);
            $html         = <<<EOF
if(typeof wx === 'object'){
    wx.openid="{$openid}";
    wx.unionid="{$unionid}";
    wx.config({$configJson});
    wx.fansinfo={$fansinfoJson};
    wx.ready(function(){
        wx.hideOptionMenu();
        wx.hideAllNonBaseMenuItem();
    });
}
EOF;

            return Response::create($html)->contentType('application/x-javascript');
        }

    }